/*
 * Copyright WelinkSoft Ltd. (c) 2020.
 */

package com.xj.server;


import com.xj.util.DateUtils;
import com.xj.util.FileUtils;
import com.xj.util.FtpdUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017-06-27.
 */
@Service
public class FtpService {
       private Logger logger = Logger.getLogger(this.getClass());
@Value(value = "backfold")
String  backfold;
@Value(value = "ftpServerAddress")
String ip;
@Value(value = "ftpServerPort")
String ports;
@Value(value = "ftpServerUser")
String user;
@Value(value = "ftpServerPassword")
String password;

@Value(value = "ftpRemoteDirectory")
String  ftpRemoteDirectory;
@Value(value = "saveday")
String day;


        public void downFile() {
            if(!backfold.endsWith(File.separator)){
                backfold=backfold+ File.separator;
            }
            int  port=21;
         if(StringUtils.isNotEmpty(ports)){
             port=Integer.valueOf(ports);
         }
        FTPClient  ftpClient = FtpdUtil.ftpLogin(ip,port,user,password);
           if (ftpClient==null) {
           logger.error("FTP登录失败！");
               return;
        }
            FtpdUtil.downLoadDirectory(ftpClient, backfold, ftpRemoteDirectory);
            FtpdUtil.ftpLogOut(ftpClient);
            if(StringUtils.isEmpty(day)){
                day="10";
            }
                deleteExpireFold(backfold,Integer.valueOf(day));
    }



    public  void  deleteExpireFold(String  backfold,int  day){
        File  bakcFolds=new File(backfold);
        File[]  files=bakcFolds.listFiles();
        for (int i=0;i<files.length;i++) {
            File  file=files[i];
            if(file.isDirectory()) {
                String  foldname = file.getName();
                Calendar calander= Calendar.getInstance();
                calander.add(Calendar.DATE,day*(-1));
                Date date=calander.getTime();
                String  expireDate= DateUtils.formatDate(date, "yyyyMMdd");
                String  today= DateUtils.formatDate(new Date(), "yyyyMMdd");
                String  todayFold=file.getParent()+File.separator+today;
                File[]  todayFolds=new File(todayFold).listFiles();
                if(file.getName().equals("20200201")){
                    System.out.println(file.getPath());
                }
                if(foldname.compareTo(expireDate)<0&&todayFolds!=null&&todayFolds.length>0){
                    FileUtils.removeDir(file);
                }else{
                    deleteExpireFold(file.getAbsolutePath(),day);
                }
            }
        }
    }


    public void uploadFile() {
        logger.info("begin upload  back  file");
        if(!backfold.endsWith(File.separator)){
            backfold=backfold+ File.separator;
        }
        if(StringUtils.isEmpty(day)){
            day="10";
        }
        int  port=21;
        if(StringUtils.isNotEmpty(ports)){
            port=Integer.valueOf(ports);
        }
        FTPClient  ftpClient = FtpdUtil.ftpLogin(ip,port,user,password);
        if (ftpClient==null) {
            logger.error("FTP登录失败！");
            return;
        }
        FtpdUtil.uploadDirectory(ftpClient, backfold, ftpRemoteDirectory);
        logger.info("upload  back file  finished!");
        FtpdUtil.deleteHisoryFile(ftpClient,ftpRemoteDirectory,day);
        logger.info("delte  validate  file  finished!");
        FtpdUtil.ftpLogOut(ftpClient);
       logger.info("back  finished  success!");
    }
}